package gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

/**
 *
 * @author Mariam
 */
public final class ConnectionWindow extends JFrame implements ActionListener {

    private JButton bConnect;
    private JLabel labUsername;
    private JTextArea textUsername;
    private ChatGUI parent;

    /**
     * Constructs a window where the user can type a login and click on connect
     * to enter into the Chat System
     *
     * @param parent the GUI which has created the window
     */
    public ConnectionWindow(ChatGUI parent) {
        super();
        initComponents();
        this.parent = parent;
    }

    private void initComponents() {
        labUsername = new JLabel("Enter your name : ");
        textUsername = new JTextArea();
        textUsername.setColumns(20);
        bConnect = new JButton("Connect");

        this.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        this.add(labUsername);
        this.add(textUsername);
        this.add(bConnect);
        this.pack();
        this.setTitle("Chat System - Connection");
        this.setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        bConnect.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (!"".equals(textUsername.getText())) {
            parent.connect(textUsername.getText());
        }
    }
}